//
//  DLMainDrawView.m
//  DontRem
//
//  Created by apple on 2021/1/26.
//

#import "DLMainDrawView.h"
#import "DLGroupManagerVC.h"
#import "DLExportExcelModel.h"
#import "DLPswModel.h"
#import <QuickLook/QuickLook.h>
@interface DLMainDrawView()<UITableViewDelegate,UITableViewDataSource,QLPreviewControllerDataSource,QLPreviewControllerDelegate,UIGestureRecognizerDelegate>
@property(nonatomic,retain)UIView * sideBar;
@property(nonatomic,retain)UITableView * myTab;
@property(nonatomic,retain)NSArray * rowArr, * excelArr;
@end
@implementation DLMainDrawView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.7];
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismiss)];
        tap.delegate = self;
        [self addGestureRecognizer:tap];
        NSString * name = nil;
        if (iPhoneX) {
            name = @"人脸登录";
        }else{
            name = @"指纹登录";
        }
        self.rowArr = @[@"分类管理",@"导出Excel",name];
        [self createUI];
    }
    return self;
}
-(void)dismiss{
    [UIView animateWithDuration:0.35 animations:^{
        self.sideBar.x = -SCREEN_WIDTH * 0.8;
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}
-(void)createUI{
    self.sideBar = [[UIView alloc]initWithFrame:Frame(-SCREEN_WIDTH * 0.8, 0, SCREEN_WIDTH * 0.8, SCREEN_HEIGHT)];
    self.sideBar.backgroundColor = [UIColor normalColor:UIColorWhite darkColor:UIColorBlack];
    [self addSubview:self.sideBar];
    
    self.myTab = [[UITableView alloc]initWithFrame:Frame(0, kTopHeight, SCREEN_WIDTH * 0.8, SCREEN_HEIGHT - kTopHeight - KTabbarSafeBottomMargin) style:UITableViewStyleGrouped];
    self.myTab.delegate = self;
    self.myTab.dataSource = self;
    self.myTab.backgroundColor = [UIColor normalColor:UIColorWhite darkColor:UIColorBlack];
    self.myTab.rowHeight = 50;
    [self.sideBar addSubview:self.myTab];
    
    [UIView animateWithDuration:0.35 animations:^{
        self.sideBar.x = 0;
    }];
}
#pragma mark 代理方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.rowArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        cell.textLabel.text = self.rowArr[indexPath.row];
        cell.textLabel.font = UIFontMake(floor(AUTO(16)));
        
    }
    if (indexPath.row ==2) {
        BOOL flag = nil;
        if (iPhoneX) {
            flag = [[DLUserDefaultModel userDefaultsModel].faceIdentify isEqualToString:@"YES"];
        }else{
            flag = [[DLUserDefaultModel userDefaultsModel].thumbIdentify isEqualToString:@"YES"];
        }
        
        UISwitch *sw = [UISwitch new];
        sw.transform = CGAffineTransformMakeScale(0.8, 0.8);
        sw.onTintColor = DLColdBlueColor;
        sw.on = flag;
        [sw addTarget:self action:@selector(changeAuthTraggle:) forControlEvents:UIControlEventValueChanged];
        [cell.contentView addSubview:sw];
        sw.sd_layout.rightSpaceToView(cell.contentView, 10).centerYEqualToView(cell.contentView).offset(5).widthIs(20).heightIs(30);
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }else{
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        DLGroupManagerVC * vc = [DLGroupManagerVC new];
        [[Mytools currentViewController].navigationController pushViewController:vc animated:YES];
        [self removeFromSuperview];
    }else if(indexPath.row == 1){
        [self generateExcel];
    }
}
-(void)changeAuthTraggle:(UISwitch*)sw{
    if (sw.on) {
        if (iPhoneX) {
            [DLUserDefaultModel userDefaultsModel].faceIdentify = @"YES";
        }else{
            [DLUserDefaultModel userDefaultsModel].thumbIdentify = @"YES";
        }
    } else {
        if (iPhoneX) {
            [DLUserDefaultModel userDefaultsModel].faceIdentify = @"NO";
        }else{
            [DLUserDefaultModel userDefaultsModel].thumbIdentify = @"NO";
        }
    }
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {
        return NO;
    }else {
        return YES;
    }
}
-(void)generateExcel{
    NSArray * array = [DLPswModel bg_findAll:DLPswRecordTab];
    NSMutableArray * dataArr = [NSMutableArray new];
    for (DLPswModel * mo in array) {
        NSMutableDictionary * dic = [NSMutableDictionary new];
        [dic setObject:mo.title forKey:@"title"];
        [dic setObject:mo.account forKey:@"account"];
        [dic setObject:mo.psw forKey:@"psw"];
        [dic setObject:mo.groupName forKey:@"groupName"];
        [dic setObject:mo.url?:@"" forKey:@"url"];
        [dic setObject:mo.remark?:@"" forKey:@"remark"];
        [dataArr addObject:dic];
    }
    DLExportExcelModel * handle = [DLExportExcelModel new];
    handle.cloumnArr = @[@"标题",@"账号",@"密码",@"分组",@"网址",@"备注"];
    handle.cloumnKeyArr = @[@"title",@"account",@"psw",@"groupName",@"url",@"remark"];
    handle.dataArr = dataArr;
    NSString * excelPath = [handle createFile];
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:excelPath]) {
        self.excelArr = @[[NSURL fileURLWithPath:excelPath]];
        QLPreviewController* qlPreview = [[QLPreviewController alloc] init];
        qlPreview.dataSource = self;
        qlPreview.delegate = self;
        [[Mytools currentViewController].navigationController pushViewController:qlPreview animated:YES];
    }
}
-(NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController*)controller{
    return self.excelArr.count; //要预览的文件的数量，类似于tableView的numberOfRow
}
-(id<QLPreviewItem>)previewController:(QLPreviewController*)controller previewItemAtIndex:(NSInteger)index{
    return self.excelArr[index];//返回需要显示的文件的本地路径
}
@end
